#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "log.hpp"
log l(1);

int main()
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        l(Error, __LINE__, "udpclient: socket error");
        exit(-1);
    }

    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = static_cast<uint16_t>(htons(8080));
    local.sin_addr.s_addr = inet_addr("150.158.44.14");

    while (true)
    {
        char server[1024];
        std::string s;
        std::cout << "发送：";
        std::cin >> s;
        ssize_t n = sendto(sockfd, s.c_str(), s.size(), 0, reinterpret_cast<struct sockaddr*>(&local), sizeof(local));

        //接收信息
        n = recv(sockfd, server, 1023, 0);
        if (n > 0)
        {
            server[n] = '\0';
            std::cout << "server: " << server << std::endl;
        }
    }

    close(sockfd);

    return 0;
}